<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserRewardRequest;
use App\Models\User;
use App\models\UserReward;
use Illuminate\Http\Request;

class UserRewardController extends Controller
{


    /**
     * Show the form for editing the specified resource.
     *
     * @param User $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        return view('resources.user.reward.edit')->with('user', $user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UserRewardRequest $request
     * @param User $user
     * @return \Illuminate\Http\Response
     */
    public function update(UserRewardRequest $request, User $user)
    {

        if ($request->hasFile('image')) {

            $path = $request->image->store('public/user-reward');
            $uri = basename($path);
            if ($user->reward) {
//                $user->reward->is_activated = $request->activate;
                $user->reward->uri = $uri;

                $user->push();
            } else {
                $user->reward()->create(['uri' => $uri,]);
            }

        }
        return redirect()->route('users.reward.edit', ['user' => $user]);
    }

}
